package com.module.lemlin.rxhttp

import com.google.gson.JsonSyntaxException
import kotlinx.coroutines.TimeoutCancellationException
import rxhttp.wrapper.exception.HttpStatusCodeException
import rxhttp.wrapper.exception.ParseException
import java.net.ConnectException
import java.net.SocketTimeoutException
import java.net.UnknownHostException
import java.util.concurrent.TimeoutException


class HttpServiceResponse<HttpData : HttpServiceData> {

    var httpData: HttpData? = null

    var status: Status = Status.START

    var throwable: Throwable? = null
}

enum class Status {
    START, SUCCESS, FAILURE
}

val Status.code: Int
    get() = when (this) {
        Status.START -> 1
        Status.SUCCESS -> 2
        Status.FAILURE -> 3
    }

val Status.msg: String
    get() = when (this) {
        Status.START -> "开始请求"
        Status.SUCCESS -> "请求成功"
        Status.FAILURE -> "请求失败"
    }

val Throwable.code: Int
    get() {
        val errorCode: String = when (this) {
            is HttpStatusCodeException -> this.statusCode //Http状态码异常
            is ParseException -> this.errorCode     //业务code异常
            is CustomException -> this.errorCode
            else -> "-1"
        }
        return try {
            errorCode.toInt()
        } catch (e: Exception) {
            -1
        }
    }

val Throwable.msg: String
    get() {
        return if (this is UnknownHostException) { //网络异常
            "当前无网络，请检查你的网络设置"
        } else if (
            this is SocketTimeoutException  //okhttp全局设置超时
            || this is TimeoutException     //rxjava中的timeout方法超时
            || this is TimeoutCancellationException  //协程超时
        ) {
            "连接超时,请稍后再试"
        } else if (this is ConnectException) {
            "网络不给力，请稍候重试！"
        } else if (this is HttpStatusCodeException) {               //请求失败异常
            "Http状态码异常${this.statusCode}"
        } else if (this is JsonSyntaxException) {  //请求成功，但Json语法异常,导致解析失败
            "数据解析失败,请检查数据是否正确"
        } else if (this is ParseException) {       // ParseException异常表明请求成功，但是数据不正确
            this.message ?: errorCode   //msg为空，显示code
        } else if (this is CustomException) {// ParseException异常表明请求成功，但是数据不正确
            this.message ?: errorCode //msg为空，显示code
        } else {
            "请求失败，请稍后再试！${this.message}"
        }
    }